package evi.com.evi.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import evi.com.evi.R;


public class BaseDialog extends Dialog {

	protected Context context;

	protected LayoutInflater inflater;

	protected OnDialogClickListener mListener;
	protected OnSelectDialogClickListener mSelectListener;
	protected OnSelectPetDialogClickListener mSelectPetListener;

	public boolean clickWithDismiss = true;

	protected TextView titleView;

	protected OnDialogCancleListener onDialogCancleListener;

	public boolean clickBtn;// trueΪȷ�ϰ�ť��falseȡ��ť

	public SelectCallBack callBack;

	public class SelectCallBack {
		public int resultCode = 0;
		public String resultInfo = "";
	}

	/**
	 * ���췽��
	 * 
	 * @param context
	 */
	public BaseDialog(Context context) {
		super(context, R.style.MyDialog);
		this.setCanceledOnTouchOutside(false);

		this.context = context;
		this.inflater = LayoutInflater.from(context);
		mListener = new OnDialogClickListener(this);
		mSelectListener = new OnSelectDialogClickListener(this);
		mSelectPetListener = new OnSelectPetDialogClickListener(this);
	}

	private class OnDialogClickListener implements
			View.OnClickListener {

		private Dialog dialog;

		public OnDialogClickListener(Dialog dialog) {
			this.dialog = dialog;
		}

		@Override
		public void onClick(View v) {
			if (null != v.getTag()) {
				OnDialogButtonClickListener odbcl = (OnDialogButtonClickListener) v
						.getTag();
				odbcl.onClick(dialog, v);
			}
		/*	if (clickWithDismiss) {
				dialog.dismiss();
			}*/
		}
	}

	public class OnSelectDialogClickListener implements
			View.OnClickListener {

		private Dialog dialog;

		public OnSelectDialogClickListener(Dialog dialog) {
			this.dialog = dialog;
		}

		@Override
		public void onClick(View v) {
			if (null != v.getTag() && callBack != null) {
				OnSelectTypeDialogClickListener odbcl = (OnSelectTypeDialogClickListener) v
						.getTag();
				odbcl.onClick(dialog, v, callBack.resultCode);
			}
			if (clickWithDismiss) {
				dialog.dismiss();
			}
		}
	}

	public class OnSelectPetDialogClickListener implements
			View.OnClickListener {

		private Dialog dialog;

		public OnSelectPetDialogClickListener(Dialog dialog) {
			this.dialog = dialog;
		}

		@Override
		public void onClick(View v) {
			if (null != v.getTag() && callBack != null) {
				OnSelectPetTypeDialogClickListener odbcl = (OnSelectPetTypeDialogClickListener) v
						.getTag();
				odbcl.onClick(dialog, v, callBack.resultInfo);
			}
			if (clickWithDismiss) {
				dialog.dismiss();
			}
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		if (onDialogCancleListener != null)
			onDialogCancleListener.onDialogCancle();
	}

	public void setOnCancleListener(OnDialogCancleListener listener) {
		onDialogCancleListener = listener;
	}

	public interface OnDialogCancleListener {
		public void onDialogCancle();
	}

	public interface OnDialogButtonClickListener {
		public void onClick(Dialog dialog, View v);
	}

	public interface OnSelectTypeDialogClickListener {
		public void onClick(Dialog dialog, View v, int selectCode);

	}

	public interface OnSelectPetTypeDialogClickListener {
		public void onClick(Dialog dialog, View v, String selectType);

	}

}
